package com.housesales.web;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import module.Employees;
import module.KPI;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import bussinessService.basicbussinessService;
import bussinessService.employeesBussinessService;
import bussinessService.impl.EmployeesBussinessServiceImpl;
import bussinessService.impl.KPIBussinessServiceImpl;
import domain.vo.EmployeesQueryObject;
import domain.vo.JSONObject;
import domain.vo.KPIQueryObject;
import domain.vo.KPIVO;
import domain.vo.Message;

@Controller
@RequestMapping("/kpi")
public class KPIController {
	private Logger logger = Logger.getLogger(KPIController.class.getName());
	/**
	 * KPI 业务层
	 */
	private basicbussinessService<KPI,KPIVO> kpiBSI = new KPIBussinessServiceImpl();
	/**
	 * 员工业务层实现类
	 */
	private employeesBussinessService empBIS = new EmployeesBussinessServiceImpl();

	
	@RequestMapping("{pathName}") 
	public ModelAndView month(@PathVariable("pathName") String pathName) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/admin/kpi/" + pathName);
		return mv;
	}
	
	@RequestMapping("/addKPI/{type}")
	public @ResponseBody Message saveKPI(@PathVariable("type") String type,HttpServletRequest request) {
		Message ms = new Message();
		String ename = request.getParameter("ename");
		String groupName = request.getParameter("groupName");
		String money = request.getParameter("money");
		String day  = request.getParameter("day");
		EmployeesQueryObject employeeQueryObject = new EmployeesQueryObject();
		employeeQueryObject.setE_name(ename);
		employeeQueryObject.setE_status(Employees.ACTIVITY);

		try {
			//校验用户是否存在
			boolean  passed = empBIS.validateUserExist(employeeQueryObject);	
			if(passed) {
				KPI kpi = new KPI();
				kpi.setEname(ename);
				kpi.setGroupName(groupName);
				kpi.setMoney(money);
				kpi.setType(type);
				kpi.setDay(day);
				kpiBSI.merageObject(kpi);
			} else {
				ms.setErrorMsg("员工不存在,请重新填写");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return ms;
	}
	
	@RequestMapping("/getKPIJSON/{type}")
	public @ResponseBody JSONObject<KPIVO> getKPIJSON(@PathVariable("type") String type){
		JSONObject<KPIVO> json = new JSONObject<KPIVO>();
		KPIQueryObject queryObject = new KPIQueryObject();
		List<KPIVO> vos = new LinkedList<KPIVO>();
		queryObject.setType(type);
		if (type.indexOf("m") != -1) {
			queryObject.setClazz("month");
		} else {
			queryObject.setClazz("quarter");
		}
		try {
			vos = kpiBSI.getObjectByQueryObject(queryObject);
			json.setRows(vos);
			json.setTotal(vos.size());
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return json;
	}
}
